package com.meida.module.bus.provider.controller;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.security.OpenHelper;
import com.meida.common.springmvc.base.BaseController;
import com.meida.module.admin.client.entity.BaseAction;
import com.meida.module.admin.provider.mapper.BaseRoleUserMapper;
import com.meida.module.admin.provider.service.BaseActionService;
import com.meida.module.bus.client.entity.srmapi.basedata.SrmService;
import com.meida.module.bus.provider.mapper.srm.SrmServiceMapper;
import com.meida.module.bus.provider.service.SrmServiceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * 其他系统接口补充接口层
 *
 * @author ccc
 * @date 2020-12-21
 */
@RestController
@RequestMapping("/bus/srm/system/")
public class SrmSystemServiceController extends BaseController<SrmServiceService, SrmService> {

    @Autowired
    private BaseActionService baseActionService;
    @Resource
    private BaseRoleUserMapper baseRoleUserMapper;
    @Resource
    private SrmServiceMapper srmServiceMapper;

    @ApiOperation(value = "查询菜单下所有操作", notes = "查询菜单下所有操作")
    @GetMapping(value = "action/list")
    public ResultBody actionList(@RequestParam(required = true) String menuId) {
        return ResultBody.ok(baseActionService.findListByMenuId(Long.parseLong(menuId)));
    }

    @ApiOperation(value = "查询用户下的角色下的权限按钮信息", notes = "查询用户下的角色下的权限按钮信息")
    @GetMapping(value = "action/user/list")
    public ResultBody userActionList() {
        List<Long> roleIdList = baseRoleUserMapper.selectRoleUserIdList(OpenHelper.getUserId());
        Map<String, Object> map = Maps.newHashMap();
        map.put("roleIds", roleIdList);
        List<BaseAction> actions = srmServiceMapper.userActionList(map);
        List<String> btns = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(actions)){
            for(BaseAction item: actions){
                btns.add(item.getActionCode());
            }
        }
        return ResultBody.ok(btns);
    }

}
